package com.daz.manager.manageStudent.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.fastjson.JSON;
import com.daz.common.Util.CommonUtil;
import com.daz.manager.teacherRelStudent.server.ITeacherRelStudentServer;
import com.daz.student.information.pojo.StudentInfoPojo;
import com.daz.student.information.server.IStudentInfoSV;
import com.daz.student.server.IStudentServer;
import com.github.pagehelper.PageInfo;
import com.opensymphony.xwork2.ActionSupport;
public class StudentAction extends ActionSupport{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@Autowired
	private IStudentServer studentServer;
	@Autowired
	private IStudentInfoSV studentInfoSV;
	@Autowired
	private ITeacherRelStudentServer relSV;

	/**
	 * ǰ��̨��������
	 */


    private String id;
    private String studentName;
    private String password;
    private String inclass;
    private String photo;
    private String sex;
    private String ago;
    private String academy;
    private String professional;
    private String counselor;
	private String studentIds;
	private String studentId;
	private String userName;
	private String Password;
	private String oldPassword;
	private String newPassword;
	private String teacherId;
	private Boolean success;
	private String grade;
	private String msg;
	private String idenTity;
	public String page;
	public String limit;
	public String query;
	private String uploadFileName;
	private String upload;
	private String photoPath;
	private String phone;
	private String email;
	private Date birth;
	private String politicalStatus;
	private String national;
	private String nativePlace;
	private String cardId;
	private String dorm;
	private String hobby;
	private String specialty;
	private String remark;
	private File file; 
	private int successNum;
	private int failureNum;
	private List failureList;
    private String uploadContentType;
	private PageInfo<StudentInfoPojo> resultList;
	private List<StudentInfoPojo> list;
	//private PageInfo<SummaryPojo> resultList;

	private List<String> isSelect = new ArrayList<>();
	private Map<String, Object> result = new HashMap<>(); 
	
	private String studentInfoListStr;
	
	public String qryStudentById(){
		Map<String, Object> searchConditionMap = new HashMap<>();
		searchConditionMap.put("studentId", studentId);
			try {
				list=studentInfoSV.qryStudentById(searchConditionMap);
				this.setSuccess(true);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
		return SUCCESS;
	}
	
	public String searchAllStudentListPage(){
		int s=0;
		int l=0;
		if(page!=null){
			s = Integer.parseInt(page);
		}
		if(limit != null){
			l = Integer.parseInt(limit);
		}
		
		Map<String, Object> map = new HashMap<String, Object>();
		if(CommonUtil.isNotBlank(studentId)){
			map.put("studentId", studentId);
		}
		if(CommonUtil.isNotBlank(studentName)){
			map.put("studentName", studentName);
		}
		if(CommonUtil.isNotBlank(sex)){
			map.put("sex", sex);
		}
		if(CommonUtil.isNotBlank(national)){
			map.put("national", national);
		}
		if(CommonUtil.isNotBlank(politicalStatus)){
			map.put("politicalStatus", politicalStatus);
		}
		if(CommonUtil.isNotBlank(grade)){
			map.put("grade", grade);
		}
		if(CommonUtil.isNotBlank(academy)){
			map.put("academy", academy);
		}
		if(CommonUtil.isNotBlank(professional)){
			map.put("professional", professional);
		}
		if(CommonUtil.isNotBlank(inclass)){
			map.put("inclass", inclass);
		}
		if(CommonUtil.isNotBlank(counselor)){
			map.put("counselor", counselor);
		}
		if(query != null){
			map.put("query",query);
		}
		try {
			resultList = studentInfoSV.searchStudentList(map, s, l);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	public String resetStudentPassword(){
		Map<String, Object> searchConditionMap = new HashMap<>();	
		searchConditionMap.put("studentId", studentId);
		searchConditionMap.put("password", studentId);
		try {
				studentInfoSV.updateStudentInfo(searchConditionMap);
				this.setSuccess(true);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				this.setSuccess(false);
				this.setMsg("密码重置错误！");
			}
		return SUCCESS;
	}
	

	public String deletStudentInfoByStudentIds(){
		
		String studentID[] = studentIds.split(",");
		for(int i = 0;i<studentID.length;i++){
			Map<String, Object> searchMap = new HashMap<>();
			searchMap.put("studentId", studentID[i]);
			try {
				studentInfoSV.deleteStudentInfo(searchMap);
				//删除关联
				relSV.deleteTeacherRelStudentByParam(searchMap);
				this.setSuccess(true);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				this.setSuccess(false);
				this.setMsg("修改失败，原始密码错误！");
				return SUCCESS;
			}
		}
		return SUCCESS;
	}
	
	public String deletStudentInfoByStudentId(){
			Map<String, Object> searchMap = new HashMap<>();
			searchMap.put("studentId", studentId);
			try {
				studentInfoSV.deleteStudentInfo(searchMap);
				this.setSuccess(true);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				this.setSuccess(false);
				this.setMsg("修改失败，原始密码错误！");
			}
		return SUCCESS;
	}
	public String updateStudentList(){
		List<StudentInfoPojo> studentInfoList = JSON.parseArray(studentInfoListStr, StudentInfoPojo.class);
		try {
			for(int i=0;i<studentInfoList.size();i++){
				StudentInfoPojo value = studentInfoList.get(i);
				//新增
				if(value.getId() == null || "".equals(value.getId())){
					Map<String, Object> searchMap = new HashMap<>();
					searchMap.put("studentId", value.getStudentId());
					
					if(studentInfoSV.qryStudentById(searchMap).size() > 0){
						this.setSuccess(false);
						this.setMsg("学号重复了！");
						return SUCCESS;
					}
					value.setPassword(value.getStudentId());
					studentInfoSV.importStudents(value);
				}
				//更新
				else{
					studentInfoSV.updateByClass(value);
				}
			}
			this.setSuccess(true);
		} catch (Exception e) {
			this.setSuccess(false);
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	public String importStudents() throws Exception {
		String[] type={".xls",".xlsx"};
		int curRow = 1;
	/*	Boolean flag=false;
        for(int i = 0;i<type.length;i++){
        	if(type[i].equals(uploadContentType)){
        		flag=true;
        		break;
        	}
        }
        if(!flag){
        	this.setSuccess(false);
        	this.setMsg("文件格式不正确，只支持.xls和.xlsx文件导入！");
        	return SUCCESS;
        }*/
        this.setSuccess(true);
        this.setSuccessNum(0);
        this.setFailureNum(0);
        failureList = new ArrayList();
      //  file = new File("C:\\Users\\Administrator\\Desktop\\新建 Microsoft Office Excel 2007 Workbook.xlsx");
        /*//学生信息导入规则
         * 学号（SUTDENT_ID）、姓名(STUDENT_NAME)、性别(SEX)、年龄(AGO)、
         * 学院(ACADEMY)、专业(PROFESSIONAL)、年级(GRADE)、班级(INCLASS)、
         * 辅导员姓名(REMARK)、政治面貌(POLITICAL_STATUS)、籍贯(NATIVE_PLACE)
         */       
        if(file.isFile()){
        	InputStream input = null;
            try {
            	
            	input = new FileInputStream(file);
            	XSSFWorkbook workBook;
            	workBook = new XSSFWorkbook(input);
				XSSFSheet sheet = workBook.getSheetAt(0);  
	            if (sheet != null)  
	            {  
	                for (int i = 1; i < sheet.getPhysicalNumberOfRows();i++)  
	                {  
	                	curRow ++;
	                	XSSFRow row = sheet.getRow(i);
	                	StudentInfoPojo value = new StudentInfoPojo();
	                    if(row.getCell(0) != null && !"".equals(row.getCell(0).toString().trim())){
	                    	value.setStudentId(row.getCell(0).toString().trim());
	                    	value.setPassword(value.getStudentId());
	                    	Map map = new HashMap();
	                    	map.put("studentId", value.getStudentId());
	                    	List list = studentInfoSV.qryStudentById(map);
	                    	if(list !=null && list.size() > 0){
	                    		this.failureNum++;
		                  //  	failureList.add(i);
		                    	continue;
	                    	}
	                    }else{
	                    	this.failureNum++;
	                    	failureList.add(i);
	                    	continue;
	                    	}
	                    
	                    if(row.getCell(1) != null && !"".equals(row.getCell(1).toString().trim())){
	                    	value.setStudentName(row.getCell(1).toString().trim());
	                    }else{
	                    	this.failureNum++;
	                    	failureList.add(i);
	                    	continue;
	                    	}
	                    if(row.getCell(2) != null && !"".equals(row.getCell(2).toString().trim())){
	                    	value.setSex(row.getCell(2).toString().trim());
	                    }
	                    if(row.getCell(3) != null && !"".equals(row.getCell(3).toString().trim())){
	                    	int age = Integer.parseInt(row.getCell(3).toString().trim());
	                    	value.setAgo(String.valueOf(age));
	                    }
	                    if(row.getCell(4) != null && !"".equals(row.getCell(4).toString().trim())){
	                    	value.setAcademy(row.getCell(4).toString().trim());
	                    }
	                    if(row.getCell(5) != null && !"".equals(row.getCell(5).toString().trim())){
	                    	value.setProfessional(row.getCell(5).toString().trim());
	                    }
	                    if(row.getCell(6) != null && !"".equals(row.getCell(6).toString().trim())){
	                    	value.setGrade(row.getCell(6).toString().trim());
	                    }
	                    if(row.getCell(7) != null && !"".equals(row.getCell(7).toString().trim())){
	                    	value.setInclass(row.getCell(7).toString().trim());
	                    }
	                    if(row.getCell(8) != null && !"".equals(row.getCell(8).toString().trim())){
	                    	value.setCounselor(row.getCell(8).toString().trim());
	                    }
	                    if(row.getCell(9) != null && !"".equals(row.getCell(9).toString().trim())){
	                    	value.setPoliticalStatus(row.getCell(9).toString().trim());
	                    }
	                    if(row.getCell(10) != null && !"".equals(row.getCell(10).toString().trim())){
	                    	value.setNational(row.getCell(10).toString().trim());
	                    }
	                    studentInfoSV.importStudents(value);
	                    this.successNum++;
						}
	                //teacherServer.addTeacherBatch(teachersList);
	                this.setSuccess(true);
	                }
    		} catch (IOException e) {
    			e.printStackTrace();
    			this.setSuccess(false);
    			msg = ""+curRow;
    		}
        }else{
        this.setSuccess(false);
        this.setMsg("文件解析失败！");
        }
		return SUCCESS;
	}

	public List<StudentInfoPojo> getList() {
		return list;
	}
	public void setList(List<StudentInfoPojo> list) {
		this.list = list;
	}
	public String getQuery() {
		return query;
	}
	public void setQuery(String query) {
		this.query = query;
	}
	public String getTeacherId() {
		return teacherId;
	}
	public void setTeacherId(String teacherId) {
		this.teacherId = teacherId;
	}
	
	public PageInfo<StudentInfoPojo> getResultList() {
		return resultList;
	}
	public void setResultList(PageInfo<StudentInfoPojo> resultList) {
		this.resultList = resultList;
	}
	public Boolean getSuccess() {
		return success;
	}

	public void setSuccess(Boolean success) {
		this.success = success;
	}
	
	public String getOldPassword() {
		return oldPassword;
	}
	public void setOldPassword(String oldPassword) {
		this.oldPassword = oldPassword;
	}
	public String getNewPassword() {
		return newPassword;
	}
	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getIdenTity() {
		return idenTity;
	}
	public void setIdenTity(String idenTity) {
		this.idenTity = idenTity;
	}
	public String getStudentId() {
		return studentId;
	}
	public void setStudentId(String studentId) {
		this.studentId = studentId;
	}
	public String getPassword() {
		return Password;
	}
	public void setPassword(String password) {
		Password = password;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	public String getPage() {
		return page;
	}
	public void setPage(String page) {
		this.page = page;
	}
	public String getLimit() {
		return limit;
	}
	public void setLimit(String limit) {
		this.limit = limit;
	}
	public List<String> getIsSelect() {
		return isSelect;
	}
	public void setIsSelect(List<String> isSelect) {
		this.isSelect = isSelect;
	}
	public Map<String, Object> getResult() {
		return result;
	}
	public void setResult(Map<String, Object> result) {
		this.result = result;
	}
	public String getGrade() {
		return grade;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public void setGrade(String grade) {
		this.grade = grade;
	}
	public String getUploadFileName() {
		return uploadFileName;
	}
	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}
	public String getUpload() {
		return upload;
	}
	public void setUpload(String upload) {
		this.upload = upload;
	}
	public String getPhotoPath() {
		return photoPath;
	}
	public void setPhotoPath(String photoPath) {
		this.photoPath = photoPath;
	}
	public File getFile() {
		return file;
	}
	public void setFile(File file) {
		this.file = file;
	}
	public String getUploadContentType() {
		return uploadContentType;
	}
	public void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}
	public int getSuccessNum() {
		return successNum;
	}
	public void setSuccessNum(int successNum) {
		this.successNum = successNum;
	}
	public int getFailureNum() {
		return failureNum;
	}
	public void setFailureNum(int failureNum) {
		this.failureNum = failureNum;
	}
	public List getFailureList() {
		return failureList;
	}
	public void setFailureList(List failureList) {
		this.failureList = failureList;
	}
	public Date getBirth() {
		return birth;
	}
	public void setBirth(Date birth) {
		this.birth = birth;
	}
	public String getPoliticalStatus() {
		return politicalStatus;
	}
	public void setPoliticalStatus(String politicalStatus) {
		this.politicalStatus = politicalStatus;
	}
	public String getNational() {
		return national;
	}
	public void setNational(String national) {
		this.national = national;
	}
	public String getNativePlace() {
		return nativePlace;
	}
	public void setNativePlace(String nativePlace) {
		this.nativePlace = nativePlace;
	}
	public String getCardId() {
		return cardId;
	}
	public void setCardId(String cardId) {
		this.cardId = cardId;
	}
	public String getDorm() {
		return dorm;
	}
	public void setDorm(String dorm) {
		this.dorm = dorm;
	}
	public String getHobby() {
		return hobby;
	}
	public void setHobby(String hobby) {
		this.hobby = hobby;
	}
	public String getSpecialty() {
		return specialty;
	}
	public void setSpecialty(String specialty) {
		this.specialty = specialty;
	}
	public String getRemark() {
		return remark;
	}
	public void setRemark(String remark) {
		this.remark = remark;
	}
	public String getStudentIds() {
		return studentIds;
	}
	public void setStudentIds(String studentIds) {
		this.studentIds = studentIds;
	}


	public String getStudentInfoListStr() {
		return studentInfoListStr;
	}


	public void setStudentInfoListStr(String studentInfoListStr) {
		this.studentInfoListStr = studentInfoListStr;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getInclass() {
		return inclass;
	}

	public void setInclass(String inclass) {
		this.inclass = inclass;
	}

	public String getPhoto() {
		return photo;
	}

	public void setPhoto(String photo) {
		this.photo = photo;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public String getAgo() {
		return ago;
	}

	public void setAgo(String ago) {
		this.ago = ago;
	}

	public String getAcademy() {
		return academy;
	}

	public void setAcademy(String academy) {
		this.academy = academy;
	}

	public String getProfessional() {
		return professional;
	}

	public void setProfessional(String professional) {
		this.professional = professional;
	}

	public String getCounselor() {
		return counselor;
	}

	public void setCounselor(String counselor) {
		this.counselor = counselor;
	}

	public String getStudentName() {
		return studentName;
	}

	public void setStudentName(String studentName) {
		this.studentName = studentName;
	}

}


